#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int ret = 0, n = maze.size(), m = maze[0].size();
        queue<pair<int, int>> q;
        vector<vector<bool>> vvb(n, vector<bool>(m, false));
        q.push({ entrance[0], entrance[1] });
        vvb[entrance[0]][entrance[1]] = true;
        bool b = false;
        while (q.size())
        {
            int num = q.size();
            for (int i = 0; i < num; i++)
            {
                auto [x, y] = q.front();
                q.pop();
                if (ret != 0 && (x == 0 || x == n - 1 || y == 0 || y == m - 1)) return ret;
                for (int k = 0; k < 4; k++)
                {
                    int a = x + dx[k], b = y + dy[k];
                    if (a >= 0 && a < n && b >= 0 && b < m && maze[a][b] == '.' && vvb[a][b] == false)
                        vvb[a][b] = true, q.push({ a, b });
                }
            }
            ret++;
        }
        return -1;
    }
};